/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.ucm;

import common.TodayDate;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import library.model.ButtonModel_DD;
import library.model.ValueModel_DD;
import library.view.DeliveryDesk;
import medium.MsgSender;

/**
 *
 * @author Donghui
 */
public class LendBook {
    
    private static LendBook local;
    private ButtonModel_DD component;
    private ValueModel_DD value;
    private JTable booklist;
    private DefaultTableModel model;
    private int selectedRow;
    private int selectedIndex;
    private JTextArea response;
    private String data[]=new String[10];
    private String bid;
    private String sql;


    public static LendBook getInstance()
    {
        if(local==null)
        {
            local=new LendBook();
            local.component=DeliveryDesk.getInstance().getButtonModel();
            local.value=DeliveryDesk.getInstance().getValueModel();
        }
        return local;
    }

    public void run()
    {
        bid=value.getBid();
        booklist=component.getBooklist();
        model=(DefaultTableModel)booklist.getModel();
        selectedRow=booklist.getSelectedRowCount();
        response=component.getResponse();

        if(selectedRow==1)
        {
            selectedIndex=booklist.getSelectedRow();

            data[0]=(String)model.getValueAt(selectedIndex, 0);
            data[1]=(String)model.getValueAt(selectedIndex, 1);
            data[2]=(String)model.getValueAt(selectedIndex, 2);
            data[3]=(String)model.getValueAt(selectedIndex, 3);
            data[4]=(String)model.getValueAt(selectedIndex, 4);

//            sql= bid+";"+data[0]+";"+data[1]+";"+data[2]+";"+data[3]+";"+data[4];
            sql= bid+";"+value.getPid()+";"+value.getName()+";"+data[1]+";"+TodayDate.getInstance().run()+";"+value.getRdate();
            MsgSender.getInstance().toNewBorrower(sql);
        }

    }

    public void response()
    {
        response=component.getResponse();
        if(response.getText().length()>0)
        {
            response.setText(response.getText()+"\n> 1 record inserted successfully");
        }
        else
        {
            response.setText("> 1 record inserted successfully");
        }
    }

    public void responseFailed()
    {
        response=component.getResponse();
        if(response.getText().length()>0)
        {
            response.setText(response.getText()+"\n> 1 record failed to insert");
        }
        else
        {
            response.setText("> 1 record failed to insert");
        }
    }



}
